<?php

namespace App\Admin\Controllers;


use App\Models\Team;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use App\Http\Controllers\Controller;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Arr;

class SiteGuideController extends Controller
{

    private $model;

    private $configs;

    private $form;

    protected $tabs = [
        [
            'text'=>'首页',
            'method'=>'home',
        ],
        /*[
            'text'=>'徵才',
            'method'=>'recruit',
        ],*/

        [
            'text'=>'其它',
            'method'=>'other',
        ],
    ];

    protected $default = 'home';



    public function __construct(\App\Models\Config $config, \Dcat\Admin\Widgets\Form $form)
    {

        $this->model = $config;
        $this->configs = $this->model->pluck('content','name')->toArray();

        $this->form = $form;
        $form->action(admin_url('site'));
        $form->disableResetButton();
    }

    public function index(Content $content){

        $content->row(function (Row $row) {
            $type = request('_t', $this->default);
            $tab = new Tab();

            foreach($this->tabs as $item){
                if($item['method'] == $type){
                    $this->$type();
                    $tab->add($item['text'], $this->form,true);
                }else{
                    $tab->addLink($item['text'], request()->fullUrlWithQuery(['_t' => $item['method']]));
                }
            }



            $row->column(12, $tab->withCard());
        });

        return $content
            ->header('单页管理');

    }

    public function home(){
        $this->form->text('config.home_slogan', '首頁标语1')->default(Arr::get($this->configs,'home_slogan',''));
        $this->form->textarea('config.home_slogan2', '首頁标语2')->default(Arr::get($this->configs,'home_slogan2',''));
        $this->form->image('config.home_pills', '药丸图')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'home_pills',''));

        $this->form->image('config.home_product_back', '产品板块背景图')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'home_product_back',''));
        $this->form->image('config.home_step_back', '舒压步骤背景图')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'home_step_back',''));



        $this->form->text('config.home_about_title','介绍标题')->default(Arr::get($this->configs,'home_about_title',''));
        $this->form->image('config.home_about_image', '介绍图片')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'home_about_image',''));
        $this->form->weditor('config.home_about_text','介绍内容')->simple()->default(Arr::get($this->configs,'home_about_text',''));

        $this->form->table('config.home_team_images','会馆图片展示', function (\Dcat\Admin\Form\NestedForm $table) {
            $table->image('image','图片')->url('upload/files')->autoUpload();
            $table->select('team_id','关联会馆')->options(Team::pluck('name','id'));
        })->saving(function ($v) {
            return json_encode($v);
        })->default(Arr::get($this->configs,'home_team_images',''));
        /*$this->form->text('config.home_foot_title','首頁底部标题')->default(Arr::get($this->configs,'home_foot_title',''));
        $this->form->weditor('config.home_foot_text','首頁底部内容')->simple()->default(Arr::get($this->configs,'home_foot_text',''));*/
    }



    public function other(){
        $this->form->image('config.recruit_box_img', '徵才图片')->url('upload/files')->autoUpload()->default(Arr::get($this->configs,'recruit_box_img',''));

    }


}
